from pygame import Surface, display, mouse, time
from pygame.sprite import Sprite
from pygame.gfxdraw import aacircle, filled_circle
from pygame.transform import smoothscale
from pygame.mixer import Sound
from random import random, choice
from numpy import clip, array as arr
from math import sin, cos, pi, sqrt
from pygame.locals import *
from copy import copy
import scenes
import bonus
health_color_factor = 50
brick_hit = Sound("sounds/block_hit.ogg")

class Text(Sprite):
    def __init__(self, font, text, color, pos):
        Sprite.__init__(self)
        self.image = font.render(text,1,color)
        self.rect = self.image.get_rect().move(pos)

class Brick(Sprite):
    def hit(self):
        c = brick_hit.play()
        self.health -= 1
        if self.health <= 0:
            self.kill()
            if random() < scenes.scene.bonus_chance:
                
                s = choice(bonus.bonuses)(self.rect.topleft)
                scenes.scene.add(s)
                scenes.scene.bonuses.add(s)
            return
        self.color = [i - health_color_factor for i in self.color]
        self.color = map(clip, self.color, [0]*3, [255]*3)
        self.image.fill(self.color)
    
    def init(self, pos=[30, 30], width=20, height=10, health=1):
        self.image = Surface((height, width))
        self.color = map(int, [health*health_color_factor + random() * 50 for i in range(3)])
        self.color = map(clip, self.color, [0]*3, [255]*3)
        self.image.fill(self.color)
        self.rect = self.image.get_rect().move(pos)
        self.health = health
        
class Ball(Sprite):
    
    def __init__(self, size=15):
        Sprite.__init__(self)
        tmp = Surface((size*4+10, size*4+10), SRCALPHA)
        filled_circle(tmp, size*2+5, size*2+5, size*2, (200, 200, 200))
        self.image = smoothscale(tmp,(size*2,size*2))
        self.radius = size
        self.rect = self.image.get_rect().move(200, 300)
        self.speed = arr([3.0, 6.0])
        self.coords = arr([200, 300])
        self.prev_pos = self.coords
    
    def move(self):
        if self.speed.dot(self.speed) < 100:
            self.speed *= 1.001
        self.prev_pos = self.coords
        self.coords += self.speed
        def i(x):
            return int(round(x))
        self.rect.center = map(i, self.coords)
        
    def collide(self, brick):
        self.speed = - self.speed
        
    def paddle_bounce(self, pad):
        self.speed[1] = -abs(self.speed[1])
        absspeed = sqrt(self.speed.dot(self.speed))
        self.speed[0] += (self.rect.centerx-pad.rect.centerx)/40.0
        self.speed *= absspeed/sqrt(self.speed.dot(self.speed))
        
    def copy(self):
        b = Ball(self.radius)
        b.coords = copy(self.coords)
        b.speed = copy(self.speed)
        b.move()
        return b
        
class Paddle(Sprite):
    def __init__(self, size=100):
        Sprite.__init__(self)
        self.image = Surface((size, 20))
        self.color = (60, 70, 60)
        self.image.fill(self.color)
        height = display.get_surface().get_height() - 50
        self.rect = self.image.get_rect().move(0, height)
        
    def update(self):
        self.rect[0] = mouse.get_pos()[0] - self.rect.width / 2
